***********************************************
* Title: rwanda_student_endline_clean_jde.do
* Author: Todd Pugatch
* Last update: June 10 2024
*
* Description: modifies and expands data on students from Rwanda entrepreneurship & teacher training endline
* Inputs: 	student_endline_clean_final_ENTREP-GENPAPERADDED_NOPIIs.dta
*			Student_Survey_cleaned_modifyv2.dta
* Outputs: student_endline_clean_jde.dta
*
* Notes: cleans student endline survey
************************************************

* Set environment 
local start=`"$S_TIME"'
clear
clear matrix
clear mata
graph drop _all
program drop _all
cap log close
set more off

* Set directories 
*global main "[SET MAIN DIRECTORY HERE]"
	global rawdata "$main/01_data/01_raw"
	global cleandata "$main/01_data/02_clean"
	global dofiles "$main/02_dofiles"
	global results "$main/03_results"
	global temp "$main/04_output"

* define exchange rate to convert all variables in FRW into USD
local xrate=868.0579 /*exchange rate on 1 July 2018: http://www.exchangerates.org.uk/USD-RWF-exchange-rate-history-full.html*/

/*******************************************************************
						LOAD AND PREPARE DATA
variable naming and label conventions: 
	--names:	use [bl/el] suffix for baseline/endline
				omit if variable is an identifier that could be used to match
					across datasets, like treatment status or school code
	--labels: 	use B/E for baseline/endline 
				use [H/T/S]Q for head teacher/teacher/student questionnaire
				--ESTQ: endline student tracking questionnaire
	--example: enrollment, as reported by head teacher at baseline in item #123
		name: enroll_bl
		label: "BHQ123: enrollment"
*******************************************************************/

qui use "$rawdata/student/student_endline_clean_final_ENTREP-GENPAPERADDED_NOPIIs.dta", clear

*********************************************************
* 		SECTION 1: identifying information				*
*********************************************************
local X "interview_starttime time_interview locationlatitude locationlongitude locationaltitude locationaccuracy loc_success enumename consent reenter_schoolid reenter_studentid_202 enum_comments instancename formdef_version key submissiondate starttime endtime visit_date_112 piiid"
foreach x in `X' {
	qui gen `x'_el=`x'
	lab var `x'_el "`x', endline"
}

/*see rwanda_student_endline_discrepancies1.do for reporting on these discrepancies identified below*/

/**********************************************************
					SCHOOL CODES:
	schoolid=current school
		--variable "school_name" is name of this school
		--beginning with "9999" is "non-baseline" student
		--missing is "dropout" student
	school_code_106=baseline school (but lots of students with missings here are "baseline_student==yes"?)
		--variable "school" is name of this school
**********************************************************/
* school id
qui gen long school_code_el=schoolid

/*correct school label errors, according to "Manual Checking Report.docx" */

/*check & correct discrepancies in school ID. Update schoolid variable only, not other school code variables.*/
count if schoolid==.
sort schoolid uniqueid
list schoolid reenter_schoolid school_name school school_code_106 uniqueid if schoolid!=reenter_schoolid

/*no endline or baseline school ID*/
count if schoolid==. & school_code_106==.

/**********************************************************
					STUDENT ID:
	uniqueid=ID number with prefix set by baseline school (school_code_106), string
	--if not in baseline, then prefix set by current school (schoolid)
	reenter_studentid_202=re-entry of student ID, numeric
***********************************************************/
/*Check & correct discrepancies in student ID. These are problematic if student ID prefix doesn't match school ID.*/
qui destring uniqueid, gen(studentid)
format schoolid school_code_106 studentid %12.0g
count if studentid!=reenter_studentid_202 

* check for duplicates of studentid
/*DECISION: assume that students with schoolid==504102 for xxxx but whose unique id begins with 
	405102 (xxx) are actually enrolled in xxxx. This is because there are 15 duplicate studentid's
	beginnign with 405102 with a matching schoolid, but no other students with school_code==504102.*/
qui drop if studentid==. /*can't drop if school_code missing, because students drop out & transfer between surveys*/ 
duplicates report studentid
duplicates list studentid
duplicates tag studentid, gen(dupes)
sort studentid schoolid
list schoolid school school_code_106 school studentid if dupes==1
forval i=1/19 {
	qui replace studentid=504102`i' if studentid==405102`i' & schoolid==504102 & dupes==1
}

/*Remaining dupes have no school information. Drop these, because there is no way to assign treatment status to them.*/ 
duplicates report studentid
duplicates list studentid
duplicates tag studentid, gen(dupes2)
sort studentid schoolid
list schoolid school school_code_106 school studentid if dupes2==1
drop if schoolid==. & school_code_106==.
drop dupes*

/*Generate a numeric variable corresponding to the school ID associated with a student ID.*/ 
/* replace "studentid" with "reenter_studentid_202" for comparability with baseline.*/
qui gen x=substr(uniqueid,1,6) if school_code_106!=3503001|school_code_106!=5030403|school_code_106!=40519134 /*exclude schools with IDs>6 digits*/
qui replace x=substr(uniqueid,1,7) if school_code_106==3503001|school_code_106==5030403
qui replace x=substr(uniqueid,1,8) if school_code_106==40519134
qui destring x, replace
count if school_code_106!=x & school_code_106!=.
sort school_code_106 studentid
list schoolid reenter_schoolid school_code_106 studentid reenter_studentid_202 school if school_code_106!=x & school_code_106!=.
qui replace studentid=reenter_studentid_202 if school_code_106==301117 /*GS Mwendo: see above*/
qui replace studentid=30513517 if schoolid==305135 & studentid==30153517
drop x

/********************************************************************************************
										TREATMENT STATUS
										
	Treatment variable is group_107. How determined:
		1. Treatment (group_107==1)
			--student's current school was treated (school was group_107==1 in baseline)
		2. Control (group_107==2)
			--student's current school was control (school was group_107==2 in baseline)
		3. Non-baseline (group_107==3)
			--students whose current school not in baseline sample (schoolid begins with "9999", school_name>209)
				--but almost all these students have baseline school code (school_code_106)
			--students not sampled at baseline(?)
		4. Dropout (group_107==4)
			--students who have dropped out at endline (except one w/ schoolid==503014--miscode?)
	
	How I will assign treatment:
		Following baseline data, define 3 treatment variables:
		1. treatment: initial treatment assignment
		2. treatment_unassgn: treatment status (unassigned replacement schools as missing)
		3. treatment_educateassgnt: treatment status (based on Educate! records, not initial assignment)
		
		Use baseline student data as source of these definitions.
		
		Then give each student in endline 2 versions of these variables [x]:
		1. [x]_el: endline treatment status, following initial assignment
			--based on baseline school (school_code_106), if available
			--otherwise, based on current school (schoolid)
		2. [x]_now_el: endline treatment status, based on current school (schoolid) only 
*****************************************************************************************************/
* define treatment variables using baseline student data
qui save "$temp/studenteltemp.dta", replace		

qui use "$rawdata/student/Student_Survey_cleaned_modifyv2.dta", clear
/*confirm that all students within a school have same treatment status*/
foreach x in treatment treatment_unassgn treatment_educateassgnt {
	qui bysort school_code: egen sd_`x'=sd(`x')
	assert sd_`x'==0 if sd_`x'!=.
}
qui egen x=tag(school_code)
qui keep if x==1
qui keep school_code treatment*
qui gen long schoolid=school_code
qui gen long school_code_106=school_code
qui save "$temp/studentbltemp.dta", replace	

* merge with treatment status, for those with baseline school code only
qui use "$temp/studenteltemp.dta", clear
merge m:1 school_code_106 using "$temp/studentbltemp.dta"
qui drop if _merge==2 /*drop schools in HT data but not student endline*/
foreach x in treatment treatment_unassgn treatment_educateassgnt {
	qui gen `x'_el=`x' if school_code_106==school_code	
}		

* merge with treatment status based on current school
drop _merge school_code treatment treatment_unassgn treatment_educateassgnt
merge m:1 schoolid using "$temp/studentbltemp.dta"

qui drop if _merge==2 /*drop schools in HT data but not student endline*/
foreach x in treatment treatment_unassgn treatment_educateassgnt {
	qui gen `x'_now_el=`x' if schoolid==school_code
	qui replace `x'_el=`x' if schoolid==school_code & school_code_106==. /*use current school's treatment status if baseline school missing*/
}
lab var treatment_el "treatment, endline (initial school assignment if available, otherwise current school)"
lab var treatment_unassgn_el "treatment, endline (initial school assignment if available, otherwise current school, unassigned replacement schools as missing)"
lab var treatment_educateassgnt_el "treatment, endline (initial school assignment if available, otherwise current school, based on E! records)"	
lab var treatment_now_el "treatment, endline (current school)"
lab var treatment_unassgn_now_el "treatment, endline (current school, unassigned replacement schools as missing)"
lab var treatment_educateassgnt_now_el "treatment, endline (current school, based on E! records)"	
drop _merge school_code treatment treatment_unassgn treatment_educateassgnt

/*data consistency checks*/
/*dropouts should have missing current treatment assignment*/
foreach x in treatment treatment_unassgn treatment_educateassgnt {
	qui replace `x'_now_el=. if dropout==1
}

/*students with no school status corresponding to baseline school sample should have no treatment assignment*/
list schoolid school_code_106 school_name school if treatment_el==.

/*SCHOOL LOCATION AND ATTRIBUTES*/
/*Province & district of school:
	Note that in baseline: 
		[province/district]: 		school [province/district], IPA code
			--but note that variables with these names in endline refer to student residence! will need to update
		[province/district]_id: 	school [province/district], Census code*/
local i=0
foreach x in province district sector cell village {
	qui gen `x'_schl_el=`x'_201
	lab var `x'_schl_el "ESQ10`i': `x' of current school (IPA codes)"
	local i=`i'+1
}
/*Other school attributes: public/private, boarding, etc.: postpone cleaning until matching with baseline*/

************************************************
* SECTION 2: Student identifying information   *
************************************************
foreach x in province district sector cell village {
	qui gen `x'_res_el=`x'
	lab var `x'_res_el "ESQ202: `x' of student permanent home (IPA codes)" 
}
qui gen rural_el=(loc_201==1)
qui gen homeowner_el=(house_201==2)
qui gen boarding_el=(board_day_202==1)
qui gen home_far_el=(dist_min_203>=60 & dist_min_203!=.)
qui gen dropout_el=(dropout==1)
qui gen dropout_yr_el=dropout_period
qui gen dropout_yr2018_el=(dropout_period==1)
qui gen dropout_yr2017_el=(dropout_period==2)
qui gen dropout_yr2016_el=(dropout_period==3)
qui gen class_dropout_el=class_dropout
forval g=1/6 {
	local i=-`g'+7
	qui gen class_dropout_S`g'_el=(class_dropout==`i')
	lab var class_dropout_S`g'_el "ESTQ: student dropped out of grade S`g'"
}
lab var rural_el "ESQ202: student's home in rural area" 
lab var homeowner_el "ESQ202: student's family owns their home"
lab var boarding_el "ESQ203: boarding student"
lab var home_far_el "ESQ205: home at least 1hr away"
lab var dropout_el "ESTQ: student is a dropout"
lab var dropout_yr_el "ESTQ: year student dropped out"
lab var dropout_yr2018_el "ESTQ: student dropped out in 2018"
lab var dropout_yr2017_el "ESTQ: student dropped out in 2017"
lab var dropout_yr2016_el "ESTQ: student dropped out in 2016"
lab var class_dropout "ESTQ: grade in which student dropped out"


************************************************
* 	SECTION 3: socioeconomic characteristics   *
************************************************
/*age, gender, parents*/
qui gen age_el=age_300
*qui replace age_topcode_el=25 if age_300>27 & age_300!=.
qui replace age_el=. if age_300==-99
qui gen female=(gender_301==2)
qui gen bothparents_el=(parents_302==3)
qui gen motheronly_el=(parents_302==2)
qui gen fatheronly_el=(parents_302==1)
qui gen orphan_el=(parents_302==4)
lab var age_el "ESQ300: age (-99 recoded to missing)"
lab var bothparents_el "ESQ302: has both parents"
lab var motheronly_el "ESQ302: has mother only"
lab var fatheronly_el "ESQ302: has father only"
lab var orphan_el "ESQ302: has neither biological parent"

/*household assets*/
qui gen dirtfloor_el=(floor_mat_303==1)
qui gen roof_iron_el=(roof_mat_304==3)
qui gen roof_cement_el=(roof_mat_304==5)
qui gen water_piped_el=(water_305==1)
qui gen water_unprotected_el=(water_305==6|water_305==7)
local assets "assets_306_Radio assets_306_Television assets_306_Telephone assets_306_Refrigerator assets_306_Bicycle assets_306_Motorcycle assets_306_PrivateCar"
qui egen assets_pct_el=rowmean(`assets')
qui pca `assets'
qui predict assets_pc1_el
qui gen cook_wood_el=(cook_307==1)
qui gen light_electric_el=(light_308==1|light_308==2)
lab var dirtfloor_el "ESQ303: floor of home made of earth/mud"
lab var roof_iron_el "ESQ304: roof of home made of iron"
lab var roof_cement_el "ESQ304: roof of home made of cement/concrete/clay/tiles"
lab var water_piped_el "ESQ305: piped water in household"
lab var water_unprotected_el "ESQ305: unprotected water source in household"
lab var assets_pct_el "ESQ306: proportion of HH assets owned, of 7"
lab var assets_pc1_el "ESQ306: asset index (1st principal component)"
lab var cook_wood_el "ESQ307: household cooking source is wood"
lab var light_electric_el "ESQ308: household light source is electricity or generator"

/*parental & sibling occupations and education*/
foreach x in fath moth {
	qui gen `x'_ag_el=(`x'_occup_309==1)
	qui gen `x'_business_el=(`x'_occup_309==2)
	qui gen `x'_pro_el=(`x'_occup_309==6)
}
qui gen moth_work_el=(moth_occup_309>=1 & moth_occup_309<=6 & moth_occup_309!=.)
qui gen parent_business_el=(fath_business_el==1|moth_business_el==1)
qui gen parent_pro_el=(fath_pro_el==1|moth_pro_el==1)
foreach x in fath moth {
	qui gen `x'_primary_el=(educ_`x'_310==1)
	qui gen `x'_secondary_el=(educ_`x'_310==2)
	qui gen `x'_tertiary_el=(educ_`x'_310==3|educ_`x'_310==4)
	qui gen `x'_primary_ormore_el=(`x'_primary_el==1|`x'_secondary_el==1|`x'_tertiary_el==1)
	qui gen `x'_secondary_ormore_el=(`x'_secondary_el==1|`x'_tertiary_el==1)
}
qui gen sib_secondary_el=(educ_sibling_310==2)
qui gen sib_tertiary_el=(educ_sibling_310==3|educ_sibling_310==4)
qui gen sib_secondary_ormore_el=(sib_secondary_el==1|sib_tertiary_el==1)
foreach x in fath moth {
	lab var `x'_ag_el "ESQ309: `x'er works in agriculture"
	lab var `x'_business_el "ESQ309: `x'er works in business"
	lab var `x'_pro_el "ESQ309: `x'er is professional/salaried"
	foreach s in primary secondary tertiary {
		lab var `x'_`s'_el "ESQ310: `x'er completed `s'"
	}
	lab var `x'_primary_ormore_el "ESQ310: `x'er completed primary or more"
	lab var `x'_secondary_ormore_el "ESQ310: `x'er completed secondary or more"
}
lab var moth_work_el "ESQ309: mother works"
lab var parent_business_el "ESQ309: at least one parent in business"
lab var parent_pro_el "ESQ309: at least one parent is professional/salaried"
lab var sib_secondary_el "ESQ310: sibling or other in HH completed secondary"
lab var sib_secondary_el "ESQ310: sibling or other in HH completed tertiary"
lab var sib_secondary_ormore_el "ESQ310: sibling or other in HH completed secondary or tertiary"

/*household businesses*/
qui gen HH_business_el=(business_311==1)
qui gen HH_business_employs_el=(bus_info_212bus_details_b__312>1 & bus_info_212bus_details_b__312!=.)
qui replace HH_business_employs_el=. if business_311!=1
lab var HH_business_el "ESQ311: household member owns a business"
lab var HH_business_employs_el "ESQ312: household business has more than 1 paid employee"

************************************************************
* 		SECTION 4: Income, business, and employment		   *
************************************************************
/*earnings: source & amount*/
qui gen earn_money_el=(earn_receive_401==1|earn_receive_401==2|earn_receive_401==3)
qui gen personal_business_el=(earn_receive_401==1|earn_receive_401==3)
qui gen employed_el=(earn_receive_401==2|earn_receive_401==3)
qui gen earn_last2mths_el=howmuch_401ae if earn_money_el==1 & howmuch_401ae!=-99
qui replace earn_last2mths_el=0 if earn_receive_401==4 /*earnings=0 if "No" to question about earning sources*/
qui gen earn_last2mths_usd_el=earn_last2mths_el/`xrate' /*exchange rate on 1 Jul 2018: http://www.exchangerates.org.uk/USD-RWF-exchange-rate-history-full.html*/
lab var earn_money_el "ESQ401: currently earning money (excluding pocket money)"
lab var personal_business_el "ESQ401: earns money from personal business"
lab var employed_el "ESQ401: earns money from employment"
lab var earn_last2mths_el "ESQ401a: earnings from business & employment, last 2 months, FRW"
lab var earn_last2mths_usd_el "ESQ401a: earnings from business & employment, last 2 months, USD"

/*business characteristics*/
qui gen business_solo_el=(type_busi_402_own_bus==1)
qui gen business_sbc_el=(type_busi_402_sbc_bus==1)
qui gen business_friends_el=(type_busi_402_meandpeers_bus==1)
qui gen business_fam_el=(type_busi_402_family_bus==1)
qui gen business_famorpeers_el=(type_busi_402_meandpeers_bus==1|type_busi_402_family_bus==1)
qui gen mainbiz_solo_el=(mosttime_busi_403==1)
qui gen mainbiz_sbc_el=(mosttime_busi_403==2)
qui gen mainbiz_friends_el=(mosttime_busi_403==3)
qui gen mainbiz_fam_el=(mosttime_busi_403==4)
qui gen numbizowners_el=owner_partner_404
qui gen buspartners_parent_el=(bus_relation_405==1)
qui gen buspartners_family_el=(bus_relation_405==1|bus_relation_405==2|bus_relation_405==3)
qui gen business_employs_el=(paid_worker_406>0 & paid_worker_406!=. & personal_business_el==1)
qui gen business_employees_el=paid_worker_406 if personal_business_el==1
lab var business_solo_el "ESQ402: business started by self"
lab var business_sbc_el "ESQ402: business started by student business club"
lab var business_friends_el "ESQ402: business started with peers"
lab var business_fam_el "ESQ402: family business"
lab var business_famorpeers_el "ESQ402: family or peer business"
lab var mainbiz_solo_el "ESQ403: main business owned by self"
lab var mainbiz_sbc_el "ESQ403: main business started by student business club"
lab var mainbiz_friends_el "ESQ403: main business with peers"
lab var mainbiz_fam_el "ESQ403: main business is family business"
lab var numbizowners_el "ESQ404: number of owners/partners in main business"
lab var buspartners_parent_el "ESQ405: main business partners: parent(s)"
lab var buspartners_family_el "ESQ405: main business partners: family"
lab var business_employs_el "ESQ406: main business has at least 1 paid employee"
lab var business_employees_el "ESQ406: employees, main business"

/*businss type*/
qui gen businesstype_ag_el=(type_busi_407==1)
qui gen businesstype_craft_el=(type_busi_407==2)
qui gen businesstype_food_el=(type_busi_407==3)
qui gen businesstype_hhprod_el=(type_busi_407==4)
qui gen businesstype_shop_el=(type_busi_407==5|type_busi_407==7)
qui gen businesstype_salon_el=(type_busi_407==6)
qui gen businesstype_ict_el=(type_busi_407==8)
qui gen businesstype_skill_el=(type_busi_407==9)
qui gen businesstype_other_el=(type_busi_407==10)
qui gen businesstype_nonag_el=(type_busi_407>=2 & type_busi_407<=10 & type_busi_407!=.)
lab var businesstype_ag "ESQ407: business type: farming/livestock"
lab var businesstype_craft "ESQ407: business type: art/craft/jewelry"
lab var businesstype_food "ESQ407: business type: food"
lab var businesstype_hhprod "ESQ407: business type: household products"
lab var businesstype_shop "ESQ407: business type: retail shop"
lab var businesstype_salon "ESQ407: business type: hair salon"
lab var businesstype_ict "ESQ407: business type: computer/ICT"
lab var businesstype_skill "ESQ407: business type: skilled trade (construction, tailoring, carpentry)"
lab var businesstype_other "ESQ407: business type: other"
lab var businesstype_nonag_el "ESQ407: business type: non-agricultural"

/*business activities*/
qui gen business_projideas_el=(busi_activ_408_writprojideas==1)
qui gen business_createprod_el=(busi_activ_408_creatproducts==1)
qui gen business_finrecords_el=(busi_activ_408_finrecords==1)
qui gen business_paidtax_el=(busi_activ_408_paidtax==1)
qui gen business_registered_el=(busi_activ_408_regwithgovt==1)
lab var business_projideas_el "ESQ408: business activity: written project ideas"
lab var business_createprod_el "ESQ408: business activity: created products"
lab var business_finrecords_el "ESQ408: business activity: kept financial records"
lab var business_paidtax_el "ESQ408: business activity: paid tax (last 12 months)"
lab var business_registered_el "ESQ408: business activity: registered with government"

/*profits in last 2 months:
	Calculate 2 measures:
	1. unadjusted
	2. reported amount, based on earnings frequency reported
		--if earnings reported as:
			annual:		divide by 6
			seasonal:	leave as is
			monthly:	multiply by 2
			weekly:		multiply by 8.6
			daily:		multiply by 44
			unpredictable:	leave as is*/
qui gen profits_last2mths_unadj_el=lasttime_profits_410 if personal_business_el==1
qui gen profits_last2mths_adj_el=profits_last2mths_unadj_el
qui replace profits_last2mths_adj_el=profits_last2mths_unadj_el/6 if often_earn_409==1
qui replace profits_last2mths_adj_el=profits_last2mths_unadj_el*2 if often_earn_409==3
qui replace profits_last2mths_adj_el=profits_last2mths_unadj_el*8.6 if often_earn_409==4
qui replace profits_last2mths_adj_el=profits_last2mths_unadj_el*44 if often_earn_409==5
qui gen profits_last2mths_unadj_usd_el=profits_last2mths_unadj_el/`xrate'
qui gen profits_last2mths_adj_usd_el=profits_last2mths_adj_el/`xrate'
lab var profits_last2mths_unadj_el "ESQ410: business profits last 2 months (unadjusted by frequency)"	
lab var profits_last2mths_unadj_usd_el "ESQ410: business profits last 2 months (USD, unadjusted by frequency)"	
lab var profits_last2mths_adj_el "ESQ409-410: business profits last 2 months (adjusted by frequency)"	
lab var profits_last2mths_adj_usd_el "ESQ409-410: business profits last 2 months (USD, adjusted by frequency)"	

/*expenses in last 2 months: calculate in same way as profits*/
qui gen expenses_last2mths_unadj_el=lasttime_expenses_410 if personal_business_el==1
qui gen expenses_last2mths_adj_el=expenses_last2mths_unadj_el
qui replace expenses_last2mths_adj_el=expenses_last2mths_unadj_el/6 if often_earn_409==1
qui replace expenses_last2mths_adj_el=expenses_last2mths_unadj_el*2 if often_earn_409==3
qui replace expenses_last2mths_adj_el=expenses_last2mths_unadj_el*8.6 if often_earn_409==4
qui replace expenses_last2mths_adj_el=expenses_last2mths_unadj_el*44 if often_earn_409==5
qui gen expenses_last2mths_unadj_usd_el=expenses_last2mths_unadj_el/`xrate'
qui gen expenses_last2mths_adj_usd_el=expenses_last2mths_adj_el/`xrate'
lab var expenses_last2mths_unadj_el "ESQ410: business expenses last 2 months (unadjusted by frequency)"	
lab var expenses_last2mths_unadj_usd_el "ESQ410: business expenses last 2 months (USD, unadjusted by frequency)"	
lab var expenses_last2mths_adj_el "ESQ409-410: business expenses last 2 months (adjusted by frequency)"	
lab var expenses_last2mths_adj_usd_el "ESQ409-410: business expenses last 2 months (USD, adjusted by frequency)"

/*wages: calculate in same was as profits, above*/
qui gen bizwages_last2mths_unadj_el=time_paidworker_411 if business_employs_el==1
qui gen bizwages_last2mths_adj_el=bizwages_last2mths_unadj_el
qui replace bizwages_last2mths_adj_el=bizwages_last2mths_unadj_el/6 if often_earn_409==1
qui replace bizwages_last2mths_adj_el=bizwages_last2mths_unadj_el*2 if often_earn_409==3
qui replace bizwages_last2mths_adj_el=bizwages_last2mths_unadj_el*8.6 if often_earn_409==4
qui replace bizwages_last2mths_adj_el=bizwages_last2mths_unadj_el*44 if often_earn_409==5
qui gen bizwages_last2mths_unadj_usd_el=bizwages_last2mths_unadj_el/`xrate'
qui gen bizwages_last2mths_adj_usd_el=bizwages_last2mths_adj_el/`xrate'
lab var bizwages_last2mths_unadj_el "ESQ411: employee wages last 2 months (unadjusted by frequency)"	
lab var bizwages_last2mths_unadj_usd_el "ESQ411: employee wages last 2 months (USD, unadjusted by frequency)"	
lab var bizwages_last2mths_adj_el "ESQ411: employee wages last 2 months (adjusted by frequency)"	
lab var bizwages_last2mths_adj_usd_el "ESQ411: employee wages last 2 months (USD, adjusted by frequency)"	

/*business origins*/
qui gen bizstartsbc_el=(stud_busi_club_412==1 & personal_business_el==1)
qui gen bizfunds_parent_el=(raise_money_413_mothfath==1 & personal_business_el==1)
qui gen bizfunds_fam_el=((raise_money_413_mothfath==1|raise_money_413_brothsis==1|raise_money_413_diffamlymemb==1) & personal_business_el==1)
qui gen bizfunds_sbc_el=(raise_money_413_sbcprofit==1 & personal_business_el==1)
qui gen bizfunds_church_el=(raise_money_413_churchmtempl==1 & personal_business_el==1)
qui gen bizfunds_oth_el=(raise_money_413_some1else==1 & personal_business_el==1)
qui gen bizfunds_self_el=(raise_money_413_raiself==1 & personal_business_el==1)
lab var bizstartsbc_el "ESQ412: business started as student business club"
lab var bizfunds_parent_el "ESQ413: raised funds for business from parent(s)"
lab var bizfunds_fam_el "ESQ413: raised funds for business from family"
lab var bizfunds_sbc_el "ESQ413: raised funds for business from student business club profit"
lab var bizfunds_church_el "ESQ413: raised funds for business from church"
lab var bizfunds_oth_el "ESQ413: raised funds for business from someone else (not family, SBC, or church)"
lab var bizfunds_self_el "ESQ413: raised funds for business by self"

/*own employment: earnings and business type
	For earnings, calculate 2 measures of monthly wage:
	1. unadjusted
	2. reported amount, based on earnings frequency reported
		--if earnings reported as:
			seasonal/monthly/unpredictable:	leave as is
			weekly:							multiply by 4.3
			daily:							multiply by 22*/
qui gen ownwages_last2mths_unadj_el=howmuch_415 if employed_el==1
qui gen ownwages_last2mths_adj_el=ownwages_last2mths_unadj_el
qui replace ownwages_last2mths_adj_el=ownwages_last2mths_unadj_el*4.3 if oftenpaid_414==3
qui replace ownwages_last2mths_adj_el=ownwages_last2mths_unadj_el*22 if oftenpaid_414==4
qui gen ownwages_last2mths_unadj_usd_el=ownwages_last2mths_unadj_el/`xrate'
qui gen ownwages_last2mths_adj_usd_el=ownwages_last2mths_adj_el/`xrate'
qui gen employed_ag_el=(type_busi_416==1 & employed_el==1)
qui gen job_holiday_el=(paid_holiday_417==1)
qui gen work_nopay_el=(withoutpay_428==2|withoutpay_428==3)
qui gen work_nopay_fam_el=(withoutpay_428==2)
qui gen chores_el=(whenhome_429_cooking==1|whenhome_429_cleaning==1|whenhome_429_washcloth==1|whenhome_429_caresibling==1|whenhome_429_getwater==1|whenhome_429_shopping==1|whenhome_429_allabove==1)
lab var ownwages_last2mths_unadj_el "ESQ415: own wage last month (unadjusted by frequency)"	
lab var ownwages_last2mths_unadj_usd_el "ESQ415: own wage last month (USD, unadjusted by frequency)"	
lab var ownwages_last2mths_adj_el "ESQ415: own wage last month (adjusted by frequency)"	
lab var ownwages_last2mths_adj_usd_el "ESQ415: own wage last month (USD, adjusted by frequency)"	
lab var employed_ag_el "ESQ416: employed in agriculture"
lab var job_holiday_el "ESQ417: employed for pay during last school holiday"
lab var work_nopay_el "ESQ428: working without pay"
lab var work_nopay_fam_el "ESQ428: working without pay for family business"
lab var chores_el "ESQ429: does household chores regularly when at home"

/*alternative income measures: sum of profits and wage income*/
foreach x in unadj adj {
	qui egen earn_alt_`x'_el=rowtotal(profits_last2mths_`x'_el ownwages_last2mths_`x'_el)
	qui egen earn_alt_`x'_usd_el=rowtotal(profits_last2mths_`x'_usd_el ownwages_last2mths_`x'_usd_el)
	lab var earn_alt_`x'_el "ESQ410/415: earnings from business & employment, last 2 months (`x'usted by frequency)"
	lab var earn_alt_`x'_usd_el "ESQ410/415: earnings from business & employment, last 2 months (USD, `x'usted by frequency)"
}

************************************************************
* 		SECTION 5: Academic background & performance	   *
************************************************************
qui gen enroll_S6_el=(class_400e==3 & dropout==0)
qui gen enroll_S5_el=(class_400e==2 & dropout==0)
qui gen enroll_S4_el=(class_400e==1 & dropout==0)
forval g=4/6 {
	lab var enroll_S`g'_el "ESQ500: currently enrolled in S`g'"
}
qui gen enrolled_S4orbefore_el=(enterlevel_502<=10 & enterlevel_502!=.)
lab var enrolled_S4orbefore_el "ESQ502: entered this school at S4 or before"
forval g=4/6 {
	qui gen repeated_S`g'_el=reptimes`g'_503e
	qui replace repeated_S`g'_el=. if reptimes`g'_503e<0
	qui gen droppedout_S`g'_el=droppeds`g'_504e
	qui replace droppedout_S`g'_el=. if droppeds`g'_504e<0
	lab var repeated_S`g'_el "ESQ503: number of times repeated S`g'"
	lab var repeated_S`g'_el "ESQ504: number of times dropped out of S`g'"
}
foreach x in repeated droppedout {
	if ("`x'"=="repeated") local i=3
	else if ("`x'"=="droppedout") local i=4
	order `x'_S?_el
	qui egen num`x'_el=rowtotal(`x'_S4_el-`x'_S6_el)
	qui gen ever`x'_el=(num`x'_el>0)
	qui replace ever`x'_el=1 if dropout==1
	lab var num`x'_el "ESQ50`i': number of times `x' S4-S6"
	lab var ever`x'_el "ESQ50`i': ever `x' S4-S6"
}
qui gen lastpromexam_el=aggr_404
qui replace lastpromexam_el=. if aggr_404<0
qui gen lastpromexam_S4_el=lastpromexam_el if proexam_505==3
qui gen lastpromexam_S5_el=lastpromexam_el if proexam_505==2
qui gen lastpromexam_S6_el=lastpromexam_el if proexam_505==1
lab var lastpromexam_el "ESQ506: score on last promotional exam"
lab var lastpromexam_S4_el "ESQ506: score on last promotional exam (S4)"
lab var lastpromexam_S5_el "ESQ506: score on last promotional exam (S5)"
lab var lastpromexam_S6_el "ESQ506: score on last promotional exam (S6)"


************************************************************************
* 				SECTION 6: Classroom experience						   *
************************************************************************
/* set equal to missing if student is a dropout, for consistency.*/
/*frequency of active pedagogical methods*/
/*note: assuming variable "skillslab_610" is really about case studies, based on its label and place in the sequence of variables surrounding it*/
local i=600
foreach x in dividclass present_group outclass { /*variables with "daily" option*/
	if ("`x'"=="dividclass") local i=600
	if ("`x'"=="present_group") local i=603
	if ("`x'"=="outclass") local i=604
	qui gen `x'_never_el=(`x'_`i'==1)
	qui gen `x'_once_el=(`x'_`i'==2)
	qui gen `x'_fewtimes_el=(`x'_`i'==3)
	qui gen `x'_monthly_el=(`x'_`i'==4)
	qui gen `x'_weekly_el=(`x'_`i'==5)
	qui gen `x'_daily_el=(`x'_`i'==6)
	qui gen `x'_atleastonce_el=(`x'_once_el==1|`x'_fewtimes_el==1|`x'_monthly_el==1|`x'_weekly_el==1|`x'_daily_el==1)
	foreach f in never once fewtimes monthly weekly daily atleastonce {
		qui replace `x'_`f'_el=. if dropout==1
	}
}	
foreach x in never once fewtimes monthly weekly atleastonce daily {
	ren dividclass_`x'_el smallgroup_`x'_el
	ren present_group_`x'_el present_`x'_el
	ren outclass_`x'_el outsideclass_`x'_el
	lab var smallgroup_`x'_el "ESQ600: frequency of work in small groups in class: `x'"
	lab var present_`x'_el  "ESQ603: frequency of small groups present to class: `x'"
	lab var outsideclass_`x'_el "ESQ604: frequency of activity outside classroom: `x'"
}
qui gen presentedinclass_el=(stoodinclass_602==1)
qui replace presentedinclass_el=. if dropout==1
lab var presentedinclass_el "ESQ602: ever presented in class (this student)"

local i=605
foreach x in roleplay clas_disc cond_res fincalcul work_comp skillslab careerplan takenote { 
	qui gen `x'_never_el=(`x'_`i'==1)
	qui gen `x'_once_el=(`x'_`i'==2)
	qui gen `x'_fewtimes_el=(`x'_`i'==3)
	qui gen `x'_monthly_el=(`x'_`i'==4)
	qui gen `x'_weekly_el=(`x'_`i'==5) 
	qui gen `x'_atleastonce_el=(`x'_once_el==1|`x'_fewtimes_el==1|`x'_monthly_el==1|`x'_weekly_el==1)
	foreach f in never once fewtimes monthly weekly atleastonce {
		qui replace `x'_`f'_el=. if dropout==1
	}
	local i=`i'+1
}	

foreach x in never once fewtimes monthly weekly atleastonce {
	ren clas_disc_`x'_el classdiscussion_`x'_el
	ren cond_res_`x'_el conductresearch_`x'_el
	ren fincalcul_`x'_el donefincalc_`x'_el
	ren work_comp_`x'_el workcomputer_`x'_el
	ren skillslab_`x'_el casereview_`x'_el
	lab var roleplay_`x'_el "ESQ605: frequency of role play in class: `x'"
	lab var classdiscussion_`x'_el "ESQ606: frequency of discussion in class: `x'"
	lab var conductresearch_`x'_el "ESQ607: frequency of conducting research in class: `x'"
	lab var donefincalc_`x'_el "ESQ608: frequency of financial calculations in class: `x'"
	lab var workcomputer_`x'_el "ESQ609: frequency of computer work in class: `x'"
	lab var casereview_`x'_el "ESQ610: frequency of case review in class: `x'"
	lab var careerplan_`x'_el "ESQ611: frequency of career planning in class: `x'"
	lab var takenote_`x'_el "ESQ612: frequency of note taking in class: `x'"
}

local active_narrow "smallgroup_atleastonce_el presentedinclass_el present_atleastonce_el"
local active_broad "`active_narrow' outsideclass_atleastonce_el roleplay_atleastonce_el classdiscussion_atleastonce_el conductresearch_atleastonce_el donefincalc_atleastonce_el workcomputer_atleastonce_el casereview_atleastonce_el careerplan_atleastonce_el"

foreach x in narrow broad {
	qui egen active_instruct_`x'_el=rowmean(`active_`x'')
	qui replace active_instruct_`x'_el=. if dropout==1
}
lab var active_instruct_narrow_el "ESQ600/602/603: active instructional methods used in class, narrow definition"
lab var active_instruct_broad_el "ESQ600/602-611: active instructional methods used in class, broad definition"

/*knowledge of Skills Lab*/
qui gen knowskillslabdef_el=(def_skillab_615==2)
qui gen knowskillslabdef_mc_el=(def_skillab_616==2)
foreach x in el mc_el {
	qui replace knowskillslabdef_`x'=. if dropout==1
}
lab var knowskillslabdef_el "ESQ615: knows definition of Skills Lab (free-form answer, 0 even if not familiar with term)"
lab var knowskillslabdef_mc_el "ESQ616: knows definition of Skills Lab (multiple choice, 0 even if not familiar with term)"

************************************************************
* 			SECTION 7: Student business clubs			   *
************************************************************
/* set equal to missing if student is a dropout, for consistency.*/
/*also: busiclub_700: dummy for active business at school 
		whosup_701: who supports student business clubs
		typesupp_702: type of support provided to student business clubs*/
/*businesses at school*/
qui gen sbcatschl_el=(stud_run_bus_703==1)
qui gen numbusinessschl_none_el=(howmany_704==6)
qui gen numbusinessschl_one_el=(howmany_704==1)
qui gen numbusinessschl_multiple_el=(howmany_704>=2 & howmany_704<=5 & howmany_704!=.)
foreach x in sbcatschl_el numbusinessschl_none_el numbusinessschl_one_el numbusinessschl_multiple_el {
	qui replace `x'=. if dropout==1
}
lab var sbcatschl_el "ESQ703: student-run business at school"
lab var numbusinessschl_none_el "ESQ704: number of active businesses at school: none"
lab var numbusinessschl_one_el "ESQ704: number of active businesses at school: one"
lab var numbusinessschl_multiple_el "ESQ704: number of active businesses at school: more than one"

/*skills learned*/
/*products learned at school (version set to missing if no products learned)*/
qui gen learnedproduct_el=(make_product_705==1)
qui replace learnedproduct_el=. if dropout==1
lab var learnedproduct_el "ESQ705: learned to make products at school"
local productlist "Candles LiquidSoap Paperbags Paperbeads Baskets pastryprodcts decorations handcrafts jewellery shoepolishcream juice paints pottery other"

foreach x in  {
	qui gen learned_`x'_el=.
	qui replace learned_`x'_el=make_product_706_`x' if make_product_705==1
	qui replace learned_`x'_el=. if dropout==1
	lab var learned_`x'_el "ESQ705: learned to make `x' at school (missing if no products learned at school)"
}

/*learned from whom*/
foreach x in entrpteacher fellowstud parents anothentrpneur {
	qui gen learnedfrom_`x'_el=.
	qui replace learnedfrom_`x'_el=fromwgom_707_`x' if make_product_705==1
	qui replace learnedfrom_`x'_el=. if dropout==1
	lab var learnedfrom_`x'_el "ESQ707: learned to make products from `x' (missing if no products learned at school)"
}
qui gen currentlyuseskill_el=(useskills_708==1)
qui replace currentlyuseskill_el=. if useskills_708==.|dropout==1
lab var currentlyuseskill_el "ESQ708: currently use skill listed in Q707"

************************************************
* 		SECTION 8: Personal finance			   *
************************************************
qui gen borrow_el=(borrow_800==1)
qui gen wait_10k_el=(moneyoffer_801==2)
qui gen wait_20k_el=(moneyoffer_802==2)
qui replace wait_10k_el=. if moneyoffer_801<0
qui replace wait_20k_el=. if moneyoffer_802<0
qui gen compound_interest_el=(savings_803==1)
local savings "savedmon_804e_hiddenboxdrawer savedmon_804e_anothpersholds savedmon_804e_groupaccount savedmon_804e_individaccount savedmon_804e_SACCOaccount savedmon_804e_phoneaccount savedmon_804e_ROSCA savedmon_804e_VSLA"
*qui egen anysavings_el=anymatch(`savings'), v(1)
qui gen savings_el=savedamt_805e
qui replace savings_el=. if savedamt_805e==-99
qui gen savings_usd_el=savings_el/`xrate'
qui gen anysavings_el=(savings_el>0 & savings_el!=.)
foreach x in savings savings_usd {
	qui gen `x'_cond_el=`x'_el if anysavings_el==1
}
lab var borrow_el "ESQ800: borrowed to take advantage of economic opportunity"
lab var wait_10k_el "ESQ801: Prefer 10k FRW one month from now to 5k FRW today"
lab var wait_20k_el "ESQ802: Prefer 20k FRW one month from now to 5k FRW today"
lab var compound_interest_el "ESQ803: answer question about compound interest correctly"
lab var savings_el "ESQ805: amount saved (RWF)"
lab var savings_usd_el "ESQ805: amount saved (USD)"
lab var savings_cond_el "ESQ805: amount saved (RWF)|savings>0"
lab var savings_usd_cond_el "ESQ805: amount saved (USD)|savings>0"
lab var anysavings_el "ESQ805: has money saved"


************************************************
* 		SECTION 9: Entrepreneurship knowledge  *
************************************************
qui gen profit_calculation_el=(proftamt_1002==30000)
qui egen incrproft_true_el=anycount(incrproft_1003_SellMorePaper incrproft_1003_IncreasPrice incrproft_1003_UseCheaperMat), v(1)
qui egen incrproft_false_el=anycount(incrproft_1003_EmploySone), v(1)
qui gen incrproft_true_pct_el=incrproft_true_el/3
qui gen incrproft_false_pct_el=incrproft_false_el
qui gen profit_definition_el=(knownoproft_1005==1)
qui gen busgrwth_indic_el=(notbusgrth_1004_increasliablt==1)
lab var profit_calculation_el "ESQ1001: can calculate profit from example"
lab var incrproft_true_el "ESQ1002: number of true ways to increase profit identified (of 3)" 	
lab var incrproft_false_el "ESQ1002: number of false ways to increase profit identified (of 1)" 
lab var incrproft_true_pct_el "ESQ1002: % of true true ways to increase profit identified (of 3)" 	
lab var incrproft_false_pct_el "ESQ1002: % of false true ways to increase profit identified (of 1)"
lab var profit_definition_el "ESQ1003: understands definition of profit"
lab var busgrwth_indic_el "ESQ1005: correctly identified that increased liabilities not an indicator of business growth"

qui egen eknowledge_index_el=rowmean(profit_calculation_el incrproft_true_pct_el profit_definition_el busgrwth_indic_el)
lab var eknowledge_index_el "ESQ1001-1005: mean of entrepreneurship knowledge questions"

************************************************
* 			SECTION 10: Aspirations			   *
************************************************
/*values of schooling_1100 incorrectly labeled. Assume following (based on 1. endline_student_survey.pdf, 
	confirmed by student_endline_survey_printable.pdf):
	1=S6
	2=TVET
	3=diploma (A1)
	4=University (A0)
	5=Masters
	6=doctorate */
qui gen planned_schl_el=0 if schooling_1100==-99
qui replace planned_schl_el=1 if schooling_1100==1
qui replace planned_schl_el=2 if schooling_1100==2
qui replace planned_schl_el=3 if schooling_1100==3|schooling_1100==4
qui replace planned_schl_el=4 if schooling_1100==5
qui replace planned_schl_el=5 if schooling_1100==6
lab def planned_schl_el 0 "no answer" 1 "S6" 2 "TVET" 3 "diploma or university (A0/A1)" 4 "master's" 5 "doctorate"
lab val planned_schl_el planned_schl_el
qui gen planned_schl_sec_el=(planned_schl_el==1)
qui gen planned_schl_tvet_el=(planned_schl_el==2)
qui gen planned_schl_univ_el=(planned_schl_el==3)
qui gen planned_schl_mast_el=(planned_schl_el==4)
qui gen planned_schl_phd_el=(planned_schl_el==5)
qui gen planned_schl_postsec_el=(planned_schl_el>=3 & planned_schl_el!=.)
lab var planned_schl_el "ESQ1100: highest level of schooling planned to complete"
lab var planned_schl_sec_el "ESQ1100: highest level of schooling planned to complete: secondary"
lab var planned_schl_tvet_el "ESQ1100: highest level of schooling planned to complete: TVET"
lab var planned_schl_univ_el "ESQ1100: highest level of schooling planned to complete: diploma or university"
lab var planned_schl_mast_el "ESQ1100: highest level of schooling planned to complete: master's"
lab var planned_schl_phd_el "ESQ1100: highest level of schooling planned to complete: doctorate"
lab var planned_schl_postsec_el "ESQ1100: plans to attend post-secondary"

/*values of occup_1101 incorrectly labeled. Assume following (based on 1. endline_student_survey.pdf,
	confirmed by student_endline_survey_printable.pdf):
	1=farming
	2=business
	3=petty trading
	4=skilled labor
	5=professional 
	6=other*/
qui gen planned_occup_ag_el=(occup_1101==1)
qui gen planned_occup_business_el=(occup_1101==2)
qui gen planned_occup_pro_el=(occup_1101==5)
qui gen planned_occup_busorpro_el=(occup_1101==2|occup_1101==5)
lab var planned_occup_ag_el "ESQ1101: planned occupation: agriculture"
lab var planned_occup_business_el "ESQ1101: planned occupation: business"
lab var planned_occup_pro_el "ESQ1101: planned occupation: professional/salaried"
lab var planned_occup_busorpro_el "ESQ1101: planned occupation: business or professional/salaried"

/*values of busnsftr_1102 appear to be incorrect, based on distribution (1=yes, but 98% answer 2). Assume, 
	based on 1. endline_student_survey.pdf: 1=no, 2=yes. 
	Confirmed by confirmed by student_endline_survey_printable.pdf.*/
qui gen planned_business_el=(busnsftr_1102==2)
lab var planned_business_el "ESQ1102: plans to start business after finishing school"

************************************************************************
* 		SECTION 11: Community participation and leadership 			   *
************************************************************************
/*other variables here: 
	large_project_1102e: type of community project
	indiv_role_1103e: role in group discussions*/
qui gen commproj_el=(comm_project_1101e==1)
qui gen commconcern_el=(concern_comm_1106e==1)
foreach x in startcomprojct talkproblmfr startclub joindclub orgzawarness {
	qui gen commconcern_`x'_el=(solveproblm_1107e_`x'==1)
	lab var commconcern_`x'_el "ESQ1107: response to community concern: `x'"
}
lab var commproj_el "ESQ1101: started community project"
lab var commconcern_el "ESQ1106: concerned about problem in community"

************************************************************************
* 					SECTION 12: Locus of control				       *
************************************************************************
/*can also code reasons for head boy/girl response and safety*/
qui gen control_univ_el=univ_1200
qui gen control_housework_el=hswork_1202
qui gen control_children_el=child_1204
qui gen control_headboy_el=head_1206
qui gen control_safe_el=safe_1208
qui gen control_univ_lowscore_el=(reas_1201==1)
qui gen control_univ_money_el=(reas_1201==3)
qui gen control_univ_dontwant_el=(reas_1201==5)
qui gen control_univ_family_el=(reas_1201==6)
qui gen control_housework_parents_el=(reas_1203==1)
qui gen control_children_god_el=(reas_1205==1)
qui gen control_children_plan_el=(reas_1205==2)
qui gen control_children_spouse_el=(reas_1205==3)
qui gen control_children_couple_el=(reas_1205==4)
foreach x in univ housework children headboy safe {
	qui replace control_`x'_el=. if control_`x'_el<1
}
qui egen control_avg_el=rowmean(control_univ_el control_housework_el control_children_el control_headboy_el control_safe_el)

lab var control_univ_el "ESQ1200: locus of control: attend university (1=no control, 10=total control)"
lab var control_housework_el "ESQ1202: locus of control: amount of housework (1=no control, 10=total control)"
lab var control_children_el "ESQ1204: locus of control: number of children (1=no control, 10=total control)"
lab var control_headboy_el "ESQ1206: locus of control: be head boy/girl (1=no control, 10=total control)"
lab var control_safe_el "ESQ1208: locus of control: safe when walking (1=no control, 10=total control)"
lab var control_avg_el "ESQ1200/1202/1204/1206/1208: locus of control average: 1=no control, 10=total control"
lab var control_univ_lowscore_el "ESQ1201: reason for university locus of control: might not have high enough points"
lab var control_univ_money_el "ESQ1201: reason for university locus of control: might not have enough money"
lab var control_univ_dontwant_el "ESQ1201: reason for university locus of control: don't want to attend"
lab var control_univ_family_el "ESQ1201: reason for university locus of control: want to start family instead"
lab var control_housework_parents_el "ESQ1203: reason for housework locus of control: parents decide"
lab var control_children_god_el "ESQ1205: reason for children locus of control: God decides"
lab var control_children_plan_el "ESQ1205: reason for children locus of control: family planning"
lab var control_children_spouse_el "ESQ1205: reason for children locus of control: spouse decides"
lab var control_children_couple_el "ESQ1205: reason for children locus of control: we decide as couple"


************************************************************
* 						SECTION 13: Grit				   *
************************************************************
/*see Duckworth et al (2007) for original grit scale*/
/*first recode items so that 1=lowest grit, 5=highest*/
qui gen ideasprojx=ideasproj_1300
qui gen diffintrestx=diffintrest_1301
qui gen passchangx=passchang_1302
qui gen newprojx=newproj_1303


foreach x in ideasproj diffintrest passchang newproj {
    qui replace `x'x=. if `x'x<1 /*replace "other" with missing*/
	recode `x'x (5=1)(4=2)(3=3)(2=4)(1=5), gen(`x'_el)
    label define lbl_`x'el 1 "very true" 2 "true" 3 "somehow true" 4 "not so true" 5 "not true"
    label values `x'_el lbl_`x'el
    drop `x'x
}


qui egen grit_raw_el=rowmean(ideasproj_el diffintrest_el passchang_el newproj_el)
qui pca ideasproj_el diffintrest_el passchang_el newproj_el
qui predict grit_pc1_el
lab var ideasproj_el "ESQ1300: New ideas and projects sometimes distract me from older projects (1=very true, 5=not true)"
lab var diffintrest_el "ESQ1301: Difficult to stay interested in project that takes long time (1=very true, 5=not true)"
lab var passchang_el "ESQ1302: My interests/passions change year to year (1=very true, 5=not true)"
lab var newproj_el "ESQ1303: I become interested in new projects every few months (1=very true, 5=not true)"
lab var grit_raw_el "ESQ1300-1303: Grit index, raw score (mean of 4 items, 1=lowest, 5=highest)"
lab var grit_pc1_el "ESQ1300-1303: Grit, first principal component"

* TEST SCORES FROM REB
/*Only Entreprepeurship and General Paper scores provided.
	See also https://rw.usembassy.gov/education-culture/rwanda-education-system/ */
qui gen entrep_grade_el=entrep_grade if entrep_grade>=0 & entrep_grade<=6
qui gen genpaper_grade_el=GenPaper_grade if GenPaper_grade==0|GenPaper_grade==1
lab var entrep_grade_el "S6 entrepreneurship exam score (0-6)"
lab var genpaper_grade_el "S6 General Paper score (0=F, 1=S)"

* save data
qui gen teacherid_el=teacherid_link
lab var teacherid_el "teacherid, endline"
lab var teacherid_link "teacherid, endline"
qui gen insample_el=1
lab var insample_el "in endline sample"

qui compress
lab data "Student endline survey (2018), modified from cleaned data"
qui save "$cleandata/student_endline_clean_jde.dta", replace	

erase "$temp/studenteltemp.dta"
erase "$temp/studentbltemp.dta"
local end=`"$S_TIME"' 
di "`start'"
di "`end'"
